package com.edu.dao;


import com.edu.bean.AreaBean;
import com.edu.bean.CityBean;
import com.edu.bean.ProvinceBean;
import com.edu.utils.DBUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.List;

public class ProvinceDao {
    private static DataSource dataSource = DBUtils.getDataSource();

    //获取省
    public static List<ProvinceBean> findProvinces() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "SELECT provinceid,province FROM provinces;";
        List<ProvinceBean> provinceList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ProvinceBean>(ProvinceBean.class));
        return provinceList;
    }

    //获取市
    public static List<CityBean> findCities(String provinceid) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "SELECT cityid,city FROM cities WHERE provinceid=?;";
        List<CityBean> cityList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<CityBean>(CityBean.class), provinceid);
        return cityList;
    }

    //获取区
    public static List<AreaBean> findAreas(String cityid) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "SELECT areaid,area FROM areas WHERE cityid=?";
        List<AreaBean> areaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<AreaBean>(AreaBean.class), cityid);
        return areaList;
    }
}
